package suggestionBox.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import suggestionBox.bean.SuggestionBox;
import suggestionBox.dao.SuggestionDao;
import user.User;
import util.dao.Pagination;

import com.opensymphony.xwork2.ActionSupport;

public class SuggestionAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public SuggestionDao suggestionDao;
	
	private SuggestionBox suggestionBox;
	
	Pagination<SuggestionBox> pagination;
	
	private List<SuggestionBox> boxList= new ArrayList<SuggestionBox>();
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Pagination<SuggestionBox> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<SuggestionBox> pagination) {
		this.pagination = pagination;
	}

	public List<SuggestionBox> getBoxList() {
		return boxList;
	}

	public void setBoxList(List<SuggestionBox> boxList) {
		this.boxList = boxList;
	}

	public SuggestionBox getSuggestionBox() {
		return suggestionBox;
	}

	public void setSuggestionBox(SuggestionBox suggestionBox) {
		this.suggestionBox = suggestionBox;
	}

	public SuggestionDao getSuggestionDao() {
		return suggestionDao;
	}

	public void setSuggestionDao(SuggestionDao suggestionDao) {
		this.suggestionDao = suggestionDao;
	}

	public String execute(){
		if(pagination == null){
			pagination = new Pagination<SuggestionBox>();
			}
		if(pagination.getSize() == 0){
				pagination.setSize(7);
		}
		if(pagination.getNo() == 0){
				pagination.setNo(1);
		}
		
		if(!this.suggestionDao.findAll().toString().equals("[]")){
			this.pagination.setDesc(true);
			this.pagination.setOrderFieldName("sugg_id");
			this.suggestionDao.browse(pagination);
		}
		System.out.println("<<<<<<<<<<"+pagination.getNo());
		return SUCCESS;
	}
	
	@SuppressWarnings("deprecation")
	public String inputmessage(){
		if(this.suggestionBox!=null){
			if(this.suggestionBox.getSugg_content()!=null&&
					!this.suggestionBox.getSugg_content().trim().equals("")){
				java.util.Date now = new Date();
				int   year   =   now.getYear()   ;  
                int   month   =   now.getMonth()   ;  
                int   d   =   now.getDate();  
                @SuppressWarnings("unused")
				int   h   =   now.getHours();  
                java.sql.Date   sd   =   new   java.sql.Date(year,month,d)   ;   
				@SuppressWarnings("unused")
				DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
				this.suggestionBox.setSugg_date(df.format(sd));
				if(ServletActionContext.getRequest().getSession().getAttribute("user")==null){
					this.suggestionBox.setSugg_user_name("匿名");
				}else{
					this.suggestionBox.setSugg_user_name(((User)ServletActionContext.getRequest().getSession().getAttribute("user")).getUser_account());
				}
				this.suggestionDao.create(suggestionBox);
				this.message="这是什么原因啊？";
			}else{
				this.message="留言失败，请重新留言！";
			}
			
		}else{
			this.message="留言失败，请重新留言！";
		}
		return SUCCESS;
	}
	
	public String intoMessageAdmin(){
		if(pagination == null){
			pagination = new Pagination<SuggestionBox>();
			}
		if(pagination.getSize() == 0){
				pagination.setSize(7);
		}
		if(pagination.getNo() == 0){
				pagination.setNo(1);
		}
		
		if(!this.suggestionDao.findAll().toString().equals("[]")){
			this.pagination.setDesc(true);
			this.pagination.setOrderFieldName("sugg_id");
			this.suggestionDao.browse(pagination);
		}
		System.out.println("<<<<<<<<<<"+pagination.getNo());
		return SUCCESS;
	}
	
	public String deleteMessage(){
		if(this.suggestionBox.getSugg_id()!=null){
			this.suggestionDao.delete(
					this.suggestionDao.findById(this.suggestionBox.getSugg_id())
			);
		}
		return SUCCESS;
	}
}
